{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e1a79f9e-c9a1-47e0-8c72-a81b5467fa79",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "531e7305-c1a8-40e4-900c-13cf2595ddba",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['110110', '120120']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# re.findall(pattern,string,flags)\n",
    "\n",
    "result = re.findall('工号是(.*?)，','我的名字是张三，工号是110110，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生')\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "68cd7e79-43a1-4af6-8d0b-b039b86a7a68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'110110'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# re.search(pattern,string,flags)\n",
    "\n",
    "result = re.search('工号是(.*?)，','我的名字是张三，工号是110110，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生').group(1)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f8537784-9c34-46fc-870c-ef0080228a44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我的名字是张三，工号是119119，职业是法外狂徒；他的名字是李四，工号是119119，职业是医生\n",
      "我的名字是张三，工号是119119，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生\n"
     ]
    }
   ],
   "source": [
    "# re.sub(pattern,replace,string,count) / re.subn(pattern,replace,string,count)\n",
    "\n",
    "result1 = re.sub('\\d+','119119','我的名字是张三，工号是110110，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生')\n",
    "\n",
    "result2 = re.subn('\\d+','119119','我的名字是张三，工号是110110，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生',1)\n",
    "\n",
    "print(result1)\n",
    "print(result2[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8109cc7a-e20e-413b-a793-03b5bc2188ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "110110\n",
      "120120\n"
     ]
    }
   ],
   "source": [
    "# re.finditer(pattern,string,flags)\n",
    "\n",
    "for item in re.finditer('\\d+','我的名字是张三，工号是110110，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生'):\n",
    "    print(item.group())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1db8ca14-6f80-4eca-bf33-9375dae25c68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['我的名字是张三，工号是', '，职业是法外狂徒；他的名字是李四，工号是', '，职业是医生']\n",
      "['我的名字是张三，工号是', '，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生']\n"
     ]
    }
   ],
   "source": [
    "# re.split(pattern,string,maxsplit)\n",
    "\n",
    "result1 = re.split('\\d+','我的名字是张三，工号是110110，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生')\n",
    "\n",
    "result2 = re.split('\\d+','我的名字是张三，工号是110110，职业是法外狂徒；他的名字是李四，工号是120120，职业是医生',maxsplit=1)\n",
    "\n",
    "print(result1)\n",
    "print(result2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d836131d-66e3-41ba-9570-cfb8de5a5f90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>品牌全名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>纤美(CICI)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>江中猴姑</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25°</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              品牌全名\n",
       "0         纤美(CICI)\n",
       "1             江中猴姑\n",
       "2    PENTAL SOFTLY\n",
       "3              25°\n",
       "4  卡士(CLASSY·KISS)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造数据\n",
    "\n",
    "df = pd.DataFrame(data=['纤美(CICI)','江中猴姑','PENTAL SOFTLY','25°', '卡士(CLASSY·KISS)'], \n",
    "                  columns=['品牌全名'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "79867c77-c6b8-4157-a002-82508f2aa164",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>品牌全名</th>\n",
       "      <th>中文品牌名_自定义函数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>纤美(CICI)</td>\n",
       "      <td>纤美</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25°</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "      <td>卡士</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              品牌全名 中文品牌名_自定义函数\n",
       "0         纤美(CICI)          纤美\n",
       "1             江中猴姑        江中猴姑\n",
       "2    PENTAL SOFTLY            \n",
       "3              25°            \n",
       "4  卡士(CLASSY·KISS)          卡士"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自定义函数\n",
    "\n",
    "def chinese_name(x):\n",
    "    result = re.findall('[一-龟]',x)\n",
    "    return ''.join(result)\n",
    "\n",
    "df['中文品牌名_自定义函数'] = df['品牌全名'].apply(chinese_name)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0cb3fa2b-3167-4ca9-86a9-613148a3ddf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>品牌全名</th>\n",
       "      <th>中文品牌名_自定义函数</th>\n",
       "      <th>中文品牌名_findall</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>纤美(CICI)</td>\n",
       "      <td>纤美</td>\n",
       "      <td>纤美</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25°</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "      <td>卡士</td>\n",
       "      <td>卡士</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              品牌全名 中文品牌名_自定义函数 中文品牌名_findall\n",
       "0         纤美(CICI)          纤美            纤美\n",
       "1             江中猴姑        江中猴姑          江中猴姑\n",
       "2    PENTAL SOFTLY                          \n",
       "3              25°                          \n",
       "4  卡士(CLASSY·KISS)          卡士            卡士"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series.str.findall(pattern, flags)\n",
    "\n",
    "df['中文品牌名_findall'] = df['品牌全名'].str.findall('[一-龟]').apply(lambda x:''.join(x))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c1d83e2c-a81a-4ded-80a9-3358640df53b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>品牌全名</th>\n",
       "      <th>中文品牌名_自定义函数</th>\n",
       "      <th>中文品牌名_findall</th>\n",
       "      <th>英文品牌名_contains</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>纤美(CICI)</td>\n",
       "      <td>纤美</td>\n",
       "      <td>纤美</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25°</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "      <td>卡士</td>\n",
       "      <td>卡士</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              品牌全名 中文品牌名_自定义函数 中文品牌名_findall  英文品牌名_contains\n",
       "0         纤美(CICI)          纤美            纤美            True\n",
       "1             江中猴姑        江中猴姑          江中猴姑           False\n",
       "2    PENTAL SOFTLY                                      True\n",
       "3              25°                                     False\n",
       "4  卡士(CLASSY·KISS)          卡士            卡士            True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series.str.contains(pattern,regex=True,flags)\n",
    "\n",
    "df['英文品牌名_contains'] = df['品牌全名'].str.contains('[a-zA-Z]',regex=True)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "35090dc4-6a47-4e6d-9c61-f7a1e517772a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>品牌全名</th>\n",
       "      <th>中文品牌名_自定义函数</th>\n",
       "      <th>中文品牌名_findall</th>\n",
       "      <th>英文品牌名_contains</th>\n",
       "      <th>英文品牌名_match</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>纤美(CICI)</td>\n",
       "      <td>纤美</td>\n",
       "      <td>纤美</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25°</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "      <td>卡士</td>\n",
       "      <td>卡士</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              品牌全名 中文品牌名_自定义函数 中文品牌名_findall  英文品牌名_contains  英文品牌名_match\n",
       "0         纤美(CICI)          纤美            纤美            True        False\n",
       "1             江中猴姑        江中猴姑          江中猴姑           False        False\n",
       "2    PENTAL SOFTLY                                      True         True\n",
       "3              25°                                     False        False\n",
       "4  卡士(CLASSY·KISS)          卡士            卡士            True        False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series.str.match(pattern, flags).str[0]\n",
    "\n",
    "df['英文品牌名_match'] = df['品牌全名'].str.match('[a-zA-Z]')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "64514d7d-34db-453f-8995-171f0246338e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:3: FutureWarning: The default value of regex will change from True to False in a future version.\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>品牌全名</th>\n",
       "      <th>中文品牌名_自定义函数</th>\n",
       "      <th>中文品牌名_findall</th>\n",
       "      <th>英文品牌名_contains</th>\n",
       "      <th>英文品牌名_match</th>\n",
       "      <th>品牌名_replace</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>纤美(CICI)</td>\n",
       "      <td>纤美</td>\n",
       "      <td>纤美</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>纤美(CICI)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>江中猴姑</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25°</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>XXXX°</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "      <td>卡士</td>\n",
       "      <td>卡士</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              品牌全名 中文品牌名_自定义函数 中文品牌名_findall  英文品牌名_contains  英文品牌名_match  \\\n",
       "0         纤美(CICI)          纤美            纤美            True        False   \n",
       "1             江中猴姑        江中猴姑          江中猴姑           False        False   \n",
       "2    PENTAL SOFTLY                                      True         True   \n",
       "3              25°                                     False        False   \n",
       "4  卡士(CLASSY·KISS)          卡士            卡士            True        False   \n",
       "\n",
       "       品牌名_replace  \n",
       "0         纤美(CICI)  \n",
       "1             江中猴姑  \n",
       "2    PENTAL SOFTLY  \n",
       "3            XXXX°  \n",
       "4  卡士(CLASSY·KISS)  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series.str.replace(pattern,replace)\n",
    "\n",
    "df['品牌名_replace'] = df['品牌全名'].str.replace('[0-9]','XX')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a57307f6-8b44-442c-ab2b-22bdee4f722f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:4: FutureWarning: The default value of regex will change from True to False in a future version.\n",
      "  after removing the cwd from sys.path.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>品牌全名</th>\n",
       "      <th>中文品牌名_自定义函数</th>\n",
       "      <th>中文品牌名_findall</th>\n",
       "      <th>英文品牌名_contains</th>\n",
       "      <th>英文品牌名_match</th>\n",
       "      <th>品牌名_replace</th>\n",
       "      <th>品牌名_extract</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>纤美(CICI)</td>\n",
       "      <td>纤美</td>\n",
       "      <td>纤美</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>纤美(CICI)</td>\n",
       "      <td>CICI</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>江中猴姑</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "      <td>PENTAL SOFTLY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25°</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>XXXX°</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "      <td>卡士</td>\n",
       "      <td>卡士</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>卡士(CLASSY·KISS)</td>\n",
       "      <td>CLASSY·KISS</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              品牌全名 中文品牌名_自定义函数 中文品牌名_findall  英文品牌名_contains  英文品牌名_match  \\\n",
       "0         纤美(CICI)          纤美            纤美            True        False   \n",
       "1             江中猴姑        江中猴姑          江中猴姑           False        False   \n",
       "2    PENTAL SOFTLY                                      True         True   \n",
       "3              25°                                     False        False   \n",
       "4  卡士(CLASSY·KISS)          卡士            卡士            True        False   \n",
       "\n",
       "       品牌名_replace    品牌名_extract  \n",
       "0         纤美(CICI)           CICI  \n",
       "1             江中猴姑            NaN  \n",
       "2    PENTAL SOFTLY  PENTAL SOFTLY  \n",
       "3            XXXX°            NaN  \n",
       "4  卡士(CLASSY·KISS)    CLASSY·KISS  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series.str.extract(pattern, flags,expand) / Series.str.extractall(pattern, flags)\n",
    "\n",
    "df['品牌名_extract'] = df['品牌全名'].str.extract(r'([a-zA-Z].*)',expand=True)\n",
    "df['品牌名_extract'] = df['品牌名_extract'].str.replace('\\)','')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "51800f93-31ce-4994-9c92-3c476ed0f7c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['一级标题', '二级标题']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 回溯引用\n",
    "\n",
    "list_temp = []\n",
    "for item in re.finditer(r'(?<=<h([1-6])>).*?(?=</h\\1>)','<h1>一级标题</h1><h2>二级标题</h2><h3>这里是错误</h4>'):\n",
    "    list_temp.append(item.group())\n",
    "\n",
    "list_temp"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
